/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/psy-testing/code/model"
	"github.com/jinzhu/gorm"
	"net/http"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

var WeChat = []*mux.Action{
	{
		Name: "Read QR Code",
		Method: "GET",
		Route: "/qrcode",
		Params:mux.Parameters{
			{
				In: mux.Query,
				Schema:&readQrCode{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &readQrCode{}
			ctx.WarnParse(p)
			sub, err := model.GetSubject(p.ID)
			if err != nil {
				if err == gorm.ErrRecordNotFound {
					http.NotFound(ctx.Response, ctx.Request)
				} else {
					ctx.ErrWarning(err)
				}
			} else {
				ctx.Session().Set("redirect", "/testing?id=" + p.ID)
				ctx.View("qrcode").With("title", sub.Title)
			}
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.WeChat},
			Summary:"扫描结果二维码扫描后跳转页面",
			Description:"该页面为公众号二维码页面，扫描后进入公众号，点击公众号文章进入测试主页面",
		},
	},
}

type readQrCode struct {
	ID string `param:"id" required:"ID 不能为空"`
}